package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.config.redis.redis.util.Redishandler0;
import cn.stylefeng.guns.core.RecoverConst;
import cn.stylefeng.guns.core.UserGradeLevel;
import cn.stylefeng.guns.modular.business.entity.UserGrade;
import cn.stylefeng.guns.modular.business.mapper.UserGradeMapper;
import cn.stylefeng.guns.modular.business.model.params.UserGradeParam;
import cn.stylefeng.guns.modular.business.model.result.UserGradeResult;
import  cn.stylefeng.guns.modular.business.service.UserGradeService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 用户等级表 服务实现类
 * </p>
 *
 * @author jwj
 * @since 2019-07-18
 */
@Service
public class UserGradeServiceImpl extends ServiceImpl<UserGradeMapper, UserGrade> implements UserGradeService {

    @Autowired
    private Redishandler0 redishandler0;

    @Override
    public void add(UserGradeParam param){
        UserGrade entity = getEntity(param);
        this.save(entity);
        //缓存
        redishandler0.cacheHash(RecoverConst.USER_GRADE_REDIS_KEY, "", UserGradeLevel.first_level.getCode() + "", JSONObject.toJSONString(entity));
    }

    @Override
    public void delete(UserGradeParam param){
        UserGrade userGrade = new UserGrade();
        userGrade.setGradeId(param.getGradeId());
        userGrade.setDeleteFlag(RecoverConst.DEL_FLAG_TRUE);
        this.updateById(userGrade);
        //缓存
        redishandler0.removeHash(RecoverConst.USER_GRADE_REDIS_KEY, "", userGrade.getLevel() + "");
    }

    @Override
    public void update(UserGradeParam param){
        UserGrade oldEntity = getOldEntity(param);
        UserGrade newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
        redishandler0.cacheHash(RecoverConst.USER_GRADE_REDIS_KEY, "", UserGradeLevel.first_level.getCode() + "", JSONObject.toJSONString(newEntity));

    }

    @Override
    public UserGradeResult findBySpec(UserGradeParam param){
        return null;
    }

    @Override
    public List<UserGradeResult> findListBySpec(UserGradeParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(UserGradeParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(UserGradeParam param){
        return param.getGradeId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private UserGrade getOldEntity(UserGradeParam param) {
        return this.getById(getKey(param));
    }

    private UserGrade getEntity(UserGradeParam param) {
        UserGrade entity = new UserGrade();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
